<?php
/**
 * Created by PhpStorm.
 * User: win7
 * Date: 2018/6/13 0013
 * Time: 9:41
 */

namespace Wx\Model;


use Think\Model;

class SendMsgModel extends Model
{
    protected $autoCheckFields = false;//设置为虚拟模型

    // 推送消息 $u_id member表用户id
    public function sendsuccess($u_id,$typename,$keyword1,$keyword2,$remark,$tourl){
        $ac = file_get_contents('https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.C('WX_API.appid').'&secret='.C('WX_API.secret'));
        $wxt = json_decode($ac,true);
        $url = 'https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=' . $wxt['access_token'];//access_token改成你的有效值
        // 根据u_id
        // 根据支付记录中的u_id获取用户openid
        $openid = getInfo('member',array('id'=>$u_id),'openid');
        $data = array(
            'first' => array(
                'value' => '尊敬的用户您好，您'.$typename.'成功，请查看。',//订单类型
                'color' => '#18a689'
            ),
            'keyword1' => array(
                'value' => $keyword1,//服务类型
                'color' => '#000'
            ),
            'keyword2' => array(
                'value' => $keyword2,//提交时间
                'color' => '#000'
            ),
            'remark' => array(
                'value' => $remark,//留言
                'color' => '#000'
            )
        );
        $template_msg=array('touser'=>$openid['openid'],'url'=>$tourl,'template_id'=>'CZNxFhIYNjFx8Kzahgndtnj9ah1F9ueyUmaUr7GiAHQ','topcolor'=>'#18a689','data'=>$data);
        $curl = curl_init($url);
        $header = array();
        $header[] = 'Content-Type: application/x-www-form-urlencoded';
        curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
        // 不输出header头信息
        curl_setopt($curl, CURLOPT_HEADER, 0);
        // 伪装浏览器
        curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.118 Safari/537.36');
        // 保存到字符串而不是输出
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        // post数据
        curl_setopt($curl, CURLOPT_POST, 1);
        // 请求数据
        curl_setopt($curl,CURLOPT_POSTFIELDS,json_encode($template_msg));
        $response = curl_exec($curl);
        curl_close($curl);
        //    echo $response;
    }//end fun

    // 推送消息 $u_id member表用户id
    public function senderror($u_id,$typename,$keyword1,$keyword2,$keyword3,$remark,$tourl){
        $ac = file_get_contents('https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.C('WX_API.appid').'&secret='.C('WX_API.secret'));
        $wxt = json_decode($ac,true);
        $url = 'https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=' . $wxt['access_token'];//access_token改成你的有效值
        // 根据u_id
        // 根据支付记录中的u_id获取用户openid
        $openid = getInfo('member',array('id'=>$u_id),'openid');
        $data = array(
            'first' => array(
                'value' => '尊敬的用户您好，您'.$typename.'失败，请查看。',//订单类型
                'color' => '#18a689'
            ),
            'keyword1' => array(
                'value' => $keyword1,//项目名称
                'color' => '#000'
            ),
            'keyword2' => array(
                'value' => $keyword2,//申请状态
                'color' => '#000'
            ),
            'keyword3' => array(
                'value' => $keyword3,//失败原因
                'color' => '#000'
            ),
            'remark' => array(
                'value' => $remark,//留言
                'color' => '#000'
            )
        );
        $template_msg=array('touser'=>$openid['openid'],'url'=>$tourl,'template_id'=>'iSy2CAWrT0JiEavEYLI3hJDLMcYOEwn4nedm_GVfi4Q','topcolor'=>'#18a689','data'=>$data);
        $curl = curl_init($url);
        $header = array();
        $header[] = 'Content-Type: application/x-www-form-urlencoded';
        curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
        // 不输出header头信息
        curl_setopt($curl, CURLOPT_HEADER, 0);
        // 伪装浏览器
        curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.118 Safari/537.36');
        // 保存到字符串而不是输出
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        // post数据
        curl_setopt($curl, CURLOPT_POST, 1);
        // 请求数据
        curl_setopt($curl,CURLOPT_POSTFIELDS,json_encode($template_msg));
        $response = curl_exec($curl);
        curl_close($curl);
        //    echo $response;
    }//end fun


    // 推送消息 $u_id member表用户id
    public function groupmsg($u_id,$first,$status,$title,$price,$tourl){
        $ac = file_get_contents('https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.C('WX_API.appid').'&secret='.C('WX_API.secret'));
        $wxt = json_decode($ac,true);
        $url = 'https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=' . $wxt['access_token'];//access_token改成你的有效值
        // 根据u_id
        // 根据支付记录中的u_id获取用户openid
        $openid = getInfo('member',array('id'=>$u_id),'openid');
        $price = $price.'元';
        $time = date('Y-m-d h:i',time());
        $data = array(
            'first' => array(
                'value' => $first,      //拼团状态
                'color' => '#18a689'
            ),
            'keyword1' => array(
                'value' => $status,     //拼团状态
                'color' => '#000'
            ),
            'keyword2' => array(
                'value' => $title,      //商品标题
                'color' => '#000'
            ),
            'keyword3' => array(
                'value' => $price,      //商品价格
                'color' => '#000'
            ),
            'keyword4' => array(
                'value' => '8',         //剩余人数
                'color' => '#000'
            ),
            'keyword5' => array(
                'value' => $time,       //时间
                'color' => '#000'
            ),
            'remark' => array(
                'value' => '感觉您的惠顾，更多拼团请点击详情！',//留言
                'color' => '#000'
            )
        );
        $template_msg=array('touser'=>$openid['openid'],'url'=>$tourl,'template_id'=>'gRdIPsvlSEGLytdfdBE2OLevwlS3rbi1hoTAZuOEjv8','topcolor'=>'#18a689','data'=>$data);
        $curl = curl_init($url);
        $header = array();
        $header[] = 'Content-Type: application/x-www-form-urlencoded';
        curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
        // 不输出header头信息
        curl_setopt($curl, CURLOPT_HEADER, 0);
        // 伪装浏览器
        curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.118 Safari/537.36');
        // 保存到字符串而不是输出
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        // post数据
        curl_setopt($curl, CURLOPT_POST, 1);
        // 请求数据
        curl_setopt($curl,CURLOPT_POSTFIELDS,json_encode($template_msg));
        $response = curl_exec($curl);
        curl_close($curl);
        //    echo $response;
    }//end fun

    // 推送消息 $u_id member表用户id
    public function arrivalmsg($u_id,$keyword1,$keyword2,$keyword3,$keyword4,$tourl){
        $ac = file_get_contents('https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.C('WX_API.appid').'&secret='.C('WX_API.secret'));
        $wxt = json_decode($ac,true);
        $url = 'https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=' . $wxt['access_token'];//access_token改成你的有效值
        // 根据u_id获取用户openid
        $openid = getInfo('member',array('id'=>$u_id),'openid');
        $data = array(
            'first' => array(
                'value' => '您的商品已到货',//订单类型
                'color' => '#18a689'
            ),
            'keyword1' => array(
                'value' => $keyword1,//商品名称
                'color' => '#000'
            ),
            'keyword2' => array(
                'value' => $keyword2,//提货码
                'color' => '#000'
            ),
            'keyword3' => array(
                'value' => $keyword3,//自提地址
                'color' => '#000'
            ),
            'keyword4' => array(
                'value' => $keyword4,//自提电话
                'color' => '#000'
            ),
            'remark' => array(
                'value' => "如有任何疑问，请联系客服",//留言
                'color' => '#000'
            )
        );
        $template_msg=array('touser'=>$openid['openid'],'url'=>$tourl,'template_id'=>'GgRySR_xzddl1ciUQxK8TLmfmbMZ0fqBiP7xmi8Ur8o','topcolor'=>'#18a689','data'=>$data);
        $curl = curl_init($url);
        $header = array();
        $header[] = 'Content-Type: application/x-www-form-urlencoded';
        curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
        // 不输出header头信息
        curl_setopt($curl, CURLOPT_HEADER, 0);
        // 伪装浏览器
        curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.118 Safari/537.36');
        // 保存到字符串而不是输出
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        // post数据
        curl_setopt($curl, CURLOPT_POST, 1);
        // 请求数据
        curl_setopt($curl,CURLOPT_POSTFIELDS,json_encode($template_msg));
        $response = curl_exec($curl);
        curl_close($curl);
        //    echo $response;
    }//end fun

    // 推送消息 $u_id member表用户id
    public function focusmsg($u_id,$keyword1,$tourl){
        $ac = file_get_contents('https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.C('WX_API.appid').'&secret='.C('WX_API.secret'));
        $wxt = json_decode($ac,true);
        $url = 'https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=' . $wxt['access_token'];//access_token改成你的有效值
        // 根据u_id获取用户openid
        $openid = getInfo('member',array('id'=>$u_id),'openid');
        $data = array(
            'first' => array(
                'value' => '历史浏览信息',//标题
                'color' => '#18a689'
            ),
            'keyword1' => array(
                'value' => "点击查看：".$keyword1,//商品名称
                'color' => '#000'
            ),
            'keyword2' => array(
                'value' => '027-87872733',//座机电话
                'color' => '#000'
            ),
            'remark' => array(
                'value' => "如有疑问，请联系客服。",//留言
                'color' => '#000'
            )
        );
        $template_msg=array('touser'=>$openid['openid'],'url'=>$tourl,'template_id'=>'OWIM06_Myq_UFZhQgwdpSc8LzP1KL4yRc4GVPD48qZE','topcolor'=>'#18a689','data'=>$data);
        $curl = curl_init($url);
        $header = array();
        $header[] = 'Content-Type: application/x-www-form-urlencoded';
        curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
        // 不输出header头信息
        curl_setopt($curl, CURLOPT_HEADER, 0);
        // 伪装浏览器
        curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.118 Safari/537.36');
        // 保存到字符串而不是输出
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        // post数据
        curl_setopt($curl, CURLOPT_POST, 1);
        // 请求数据
        curl_setopt($curl,CURLOPT_POSTFIELDS,json_encode($template_msg));
        $response = curl_exec($curl);
        curl_close($curl);
        //    echo $response;
    }//end fun
}//end class